PAY = {
	urls:{
		tenpay_jsapi 	: "/pay/tenpay_jsapi" ,
		tenpay_qrcode 	: "/pay/tenpay_qrcode" ,
		tenpay_qrcode_link 	: "/pay/tenpay_qrcode_link" ,
		alipay_qrcode 	: "/pay/alipay_qrcode" ,
		alipay_jsapi 	: "/pay/alipay_wappay" ,
		alipay_qrcode_link 	: "/pay/alipay_qrcode_link" ,
		member 			: "/pay/member" ,
		query 			: "/pay/query" ,
	},
	init:function(){
		var order_id = 0;
		var urls = PAY.urls;
		//pay jsapi
		POST.init(document,{
			method:'bc-pay-tenpay',
			stop:function(){
				PAY.channel = 'tenpay';
				order_id = PAY.order_id;
				if(!order_id){
					return true;
				}
			},
			url:WEIXIN.is()?urls.tenpay_jsapi:urls.tenpay_qrcode,
			input:function(){
				body = PAY.body;
				return {order_id:order_id,body:body};
			},
			text:'准备支付',
			success:function(e,res){
				if(WEIXIN.is()){
					var data = res.data;
					$(document).trigger('bc-pay-tenpay-jsapi-ready');
					wx.chooseWXPay({
					    timestamp: data.timeStamp,
					    nonceStr: data.nonceStr, 
					    package: data.package, 
					    signType: data.signType, 
					    paySign: data.paySign,
					    // error:function(res){
					    	// alert(1)
							// $(document).trigger('bc-pay-tenpay-jsapi-cancel');
					    // }
					});
					PAY.paying.start(order_id);
				}else{
					PAY.paying.start(order_id);
					var e = $('.modal#bc-pay-tenpay')
					if(!e.length){
						var template = "<div class='modal' id='bc-pay-tenpay'><div class='modal-dialog' style='text-align:center;color:#666;padding:50px 0;'><img style='width:200px;' /><h1 style='color:maroon;text-align:center'>¥ <price price=f></price></h1><small><i class='i i-weixin'></i> 使用微信扫描二维码支付</small></div></div>"
						$('body').append(template);
						e = $('.modal#bc-pay-tenpay')
						e.find('img').on('load', function(event) {
							MODAL.open(e);
						});
						e.on('bc-modal-close', function(event) {
							PAY.paying.stop();
						});
					}
					e.find('img').attr('src',PAY.urls.tenpay_qrcode_link+"?code="+encodeURIComponent(res.data.code_url));
					if(res.data.total_fee){VALUE.write(e.find('price'),'price',res.data.total_fee);}
				}
			}
		})
		POST.init(document,{
			method:'bc-pay-alipay',
			stop:function(){
				PAY.channel = 'alipay';
				order_id = PAY.order_id;
				if(!order_id){
					return true;
				}
			},
			url:ISMOBILE()?urls.alipay_jsapi:urls.alipay_qrcode,
			input:function(){
				body = PAY.body;
				return {order_id:order_id,body:body};
			},
			text:'准备支付',
			success:function(e,res){
				if(ISMOBILE()){
					$("body").html(res.data.url);
				}else{
					PAY.paying.start(order_id);
					var e = $('.modal#bc-pay-alipay')
					if(!e.length){
						var template = "<div class='modal' id='bc-pay-alipay'><div class='modal-dialog' style='text-align:center;color:#666;padding:50px 0;'><img style='width:200px;' /><h1 style='color:maroon;text-align:center'>¥ <price price=f></price></h1><small><i class='i i-alipay'></i> 使用支付宝扫描二维码支付</small></div></div>"
						$('body').append(template);
						e = $('.modal#bc-pay-alipay')
						e.find('img').on('load', function(event) {
							MODAL.open(e);
						});
						e.on('bc-modal-close', function(event) {
							PAY.paying.stop();
						});
					}
					e.find('img').attr('src',PAY.urls.alipay_qrcode_link+"?code="+encodeURIComponent(res.data.code));
					if(res.data.total_fee){VALUE.write(e.find('price'),'price',res.data.total_fee);}
					// $("body").html(res.data.url);
				}
			},
		})
	},
	paying:{
		order_id : 0,
		start:function(order_id){
			if(this.order_id){
				this.order_id = order_id;
				return;
			}
			this.order_id = order_id;
			this.loading();
		},
		loading:function(){
			var self = this;
			if(!self.order_id) return;
			// alert(self.order_id);
			$.ajax({
				url: PAY.urls.query,
				data: {order_id: self.order_id},
                xhrFields:{
                    withCredentials:true
                },
				success:function(res){
					if(res.state){
						$(document).trigger('bc-pay-success',self.order_id)
						$(document).trigger('bc-pay-query-success',self.order_id)
						self.order_id = 0;
						TOAST.success('支付成功',1,3);
						return
					}
					setTimeout(function(){
						self.loading();
					},500)
				}
			})
		},
		stop:function(){
			this.order_id = 0;
		}
	}
}
$(function(){
	PAY.init();	
})